<template>
  <div class="lb-dy-comment">
    <top-nav></top-nav>
    <div class="page-main">
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="180"> </el-table-column>
        <el-table-column prop="user_id" label="用户ID" width="180">
        </el-table-column>
        <el-table-column prop="timeline_id" :label="tips[1]"> </el-table-column>
        <el-table-column prop="title" :label="tips[2]"> </el-table-column>
        <el-table-column prop="content" label="留言内容"> </el-table-column>
        <el-table-column prop="address" label="评论时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="confirmDel(scope.row.id)"
                >{{ $t('action.delete') }}</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="page"
        :pageSize="limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tips: { 1: `${this.$t('menu.Dynamic')}ID`, 2: `${this.$t('menu.Dynamic')}标题` },
      loading: false,
      tableData: [],
      page: 1,
      limit: 10,
      total: 0
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    getTableDataList () {
      this.loading = true
      let { limit, page } = this
      this.$api.getCommoentList({ limit, page }).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.delCommentInfo({ id }).then(res => {
          console.log(res)
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successDel'))
            this.page = this.page < Math.ceil((this.total - 1) / this.limit) ? this.page : Math.ceil((this.total - 1) / this.limit)
            this.getTableDataList()
          }
        })
      }).catch()
    },
    handleSizeChange (val) {
      this.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-dy-comment {
  width: 100%;
  .form-search {
    width: 100%;
    height: 104px;
    background: $bgThemeColor;
    margin-top: 10px;
    display: flex;
    align-items: center;
    padding-left: 30px;
    margin-bottom: 20px;
    .el-form {
      margin-top: 10px;
    }
  }
}
</style>
